package thread;

//单例模式
//懒汉模式
class SingletonLazy {
    private static volatile SingletonLazy instance = null;
    private static Object locker = new Object();

    // 第一次使用这个类的时候，才创建实例，创建时机是更晚的
    public static SingletonLazy getInstance() {
        if (instance == null) {
            synchronized (locker) {
                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }

        return instance;
    }
}

public class demo29 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1 == s2);
    }
}
